// pages/login/login.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNumber: null,
    identifyingCode: null,
    btnType: 1,
    defaultWaitTime: 60,
    waitTime: null,
    defaultWaitText: "验证",
    waitInterval: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this    
    that.setData({
      icons: wx.getStorageSync('site') + '/addons/sd_liferuning/tp/public/uploads/background',
    })

  },
  changeUserType: function (e) {
    var that = this;
    that.setData({
      userType: e.currentTarget.dataset.usertype
    });
  },
  changeInputData: function (e) {
    var that = this;
    var inputName = e.currentTarget.dataset.name;
    var inputVlaue = e.detail.value;
    if (inputName == "phoneNumber") {
      that.setData({
        phoneNumber: inputVlaue
      });
    }
    if (inputName == "identifyingCode") {
      that.setData({
        identifyingCode: inputVlaue
      });
    }
  },
  getIdentifyingCode: function () {
    var that = this;
    var reg = new RegExp('^[1][3,5,8][0-9]{9}$');
    if (!reg.test(that.data.phoneNumber)) {
      wx.showToast({
        title: '手机号不正确',
        icon: 'none',
        mask: true,
      });
      return false;
    }

    that.setData({
      btnType: 0,
      waitTime: that.data.defaultWaitTime,
      waitInterval: setInterval(function () {
        if (that.data.waitTime <= 0) {
          clearInterval(that.data.waitInterval);
          that.setData({
            btnType: 1
          });
        }
        that.setData({
          waitTime: --that.data.waitTime
        });
      }, 1000)
    });
    // TODO 验证码获取
    wx.request({
      url: '/',
      success: function () {

      }
    })
  },
  sendRequest: function () {
    var that = this;
    if (that.validate()) {
      // TODO 发送请求
      wx.request({
        url: '/',
        success: function (msg) {
        }
      })
    }
  },
  validate: function () {
    var that = this;
    var reg = new RegExp('^[1][3,5,8][0-9]{9}$');

    if (!reg.test(that.data.phoneNumber)) {
      wx.showToast({
        title: '手机号不正确',
        icon: 'none',
        mask: true,
      });
      return false;
    }

    if (!that.data.identifyingCode || that.data.identifyingCode.length <= 0) {
      wx.showToast({
        title: '验证码不得为空',
        icon: 'none',
        mask: true,
      });
      return false;
    }

    return true;
  }
})